﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.IO;

namespace ImdbRipperBrowser.Tests
{
  [TestFixture]
  public class MovieRepositoryTests
  {
    public string MoviesPath { get; set; }

    public string MoviesWithInvalidXmlPath { get; set; }

    [SetUp]
    public void SetUp()
    {
      this.MoviesPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestData/Movies");
      this.MoviesWithInvalidXmlPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestData/MoviesWithInvalidXml");
    }

    [Test]
    public void FindAllShouldReturnCorrectNumberOfMovies()
    {
      const int NUMBER_OF_MOVIES = 5;

      var repo = new MovieRepository(this.MoviesPath);

      Assert.AreEqual(NUMBER_OF_MOVIES, repo.FindAll().Count());
    }

    [Test]
    public void FindAllShouldReturnCorrectMovieIds()
    {
      string[] movieIds = new string[] { "A Perfect World.xml", "A Serious Man.xml", "Agora.xml", "Away We Go.xml", "Once Upon a Time in Mexico.xml" };

      var repo = new MovieRepository(this.MoviesPath);

      var movies = repo.FindAll();

      foreach (var movieId in movieIds)
        Assert.IsTrue(movies.Where(m => m.Id.Equals(movieId)).Count().Equals(1), string.Format("Movie Id \"{0}\" not found", movieId));
    }

    [Test]
    public void FindAllShouldReturnCorrectMovieTitles()
    {
      string[] movieTitles = new string[] { "A Perfect World", "A Serious Man", "Agora", "Away We Go", "Once Upon a Time in Mexico" };

      var repo = new MovieRepository(this.MoviesPath);

      var movies = repo.FindAll();

      foreach (var movieTitle in movieTitles)
        Assert.IsTrue(movies.Where(m => m.Title.Equals(movieTitle)).Count().Equals(1), string.Format("Movie Title \"{0}\" not found", movieTitle));
    }

    [Test]
    public void ValuesForMovieMustBeCorrectlyLoaded()
    {
      var movieIdToLoad = "Once Upon a Time in Mexico.xml";
      var title = "Once Upon a Time in Mexico";
      var year = 2003;
      var imdbUrl = @"http://www.imdb.com/title/tt0285823";
      float rating = 6.2F;
      var genre = "Action/Thriller/Western";
      var summary = @"In this sequel to 'Desperado', a Mexican drug lord pretends to overthrow the Mexican government, and is connected to a corrupt CIA agent who at that time, demands retribution from his worst enemy to carry out the drug lord's uprising against the government. Written by AnonymousOnce upon a time... it is a time of trouble is Mexico. The Mexican president has declared war on the Barillo drug cartel, who has plans to fight back by backing a coup by General Marquez to overthrow the government. Agent Sands of the CIA is in the middle of it all trying to make sure Marquez do NOT succeed... by forcing the notorious hit man-for-hire El Mariachi (Antonio Bandares) out of retirement. El Mariachi, after the death of his wife and daughter years earlier (from the previous film 'Desperado'), no longer wishes to fight, but he may not have a choice. At Culiacan, Barillo and Marquez have set a trap for the president, and Sands want El Mariachi to take out Marquez after the coup. El Mariachi called upon his two brothers for the final showdown... To save the revolution. Who will remain standing at the end? Written by K. ChangThe film is based on the story of a drug lord Barrillo, who pretends to overthrow the Mexican president. Within the drama, another of the principal persons is a corrupt CIA agent Sands who at that time, demands retribution from his worst enemy in order to carry out the drug lord's uprising against the government. El Mariachi (Antonio Bandares) is recruited by Sands to take out General Marquez (has a huge score to settle with the Mariachi), who has major beef with Mariachi to begin with. He sends Cucuy (the muscle for Sands) to shadow and keep tabs on the Mariachi. Marquez is working with Barrillo, and is about to assassinate the president and assume power. Sands also convinces Jorge Ramirez (a retired FBI agent, who has old scores to settle with Barrillo) to come out of retirement and take out Barrillo and Dr. Guevera (personal Dr. to Barrillo, killed Jorge's partner a long time ago), who brutally tortured and killed Ramirez' FBI partner. Written by Nadusha <rodriguez@al.ru>";
      var directors = "Robert Rodriguez";
      var writers = "Robert Rodriguez => written by";
      var cast = "Antonio Banderas => El Mariachi/Salma Hayek => Carolina/Johnny Depp => Sands/Mickey Rourke => Billy/Eva Mendes => Ajedrez/Danny Trejo => Cucuy/Enrique Iglesias => Lorenzo/Marco Leonardi => Fideo/Cheech Marin => Belini/Rubén Blades => Jorge FBI";

      var repo = new MovieRepository(this.MoviesPath);

      var movies = repo.FindAll();

      var movie = movies.Where(m => m.Id.Equals(movieIdToLoad)).First();

      Assert.AreEqual(title, movie.Title);
      Assert.AreEqual(year, movie.Year);
      Assert.AreEqual(imdbUrl, movie.ImdbUrl);
      Assert.AreEqual(rating, movie.Rating);
      Assert.AreEqual(genre, movie.GenresList.ToString());
      Assert.AreEqual(summary, movie.Summary.ToString());
      Assert.AreEqual(directors, movie.DirectorsList.ToString());
      Assert.AreEqual(writers, movie.WritersList.ToString());
      Assert.AreEqual(cast, movie.CastList.ToString());
    }

    [Test]
    public void MoviesWithInvalidXmlShouldBeSuccessfullyConverted()
    {
      var repo = new MovieRepository(this.MoviesWithInvalidXmlPath);

      try
      {
        var movies = repo.FindAll();
        Assert.Pass();
      }
      catch (Exception ex)
      {
        Assert.Fail();
      }
    }

  }
}
